package com.example.smart_city_0829.activity.lawyer_consultation;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smart_city_0829.R;
import com.example.smart_city_0829.base.BaseActivity;
import com.example.smart_city_0829.base.CommonAdapter;
import com.example.smart_city_0829.entity.lawyer_onsultation.Consultation;
import com.example.smart_city_0829.retrofit.NetCallBack;
import com.example.smart_city_0829.utils.Constant;

import java.util.List;
import java.util.stream.Collectors;

import retrofit2.Call;
import retrofit2.Response;

public class ConsultationListActivity extends BaseActivity {

    private RadioGroup radioGroup;
    private ListView lv;
    private TextView noDataTv;


    private List<Consultation.RowsBean> data;

    private int pageNum = 1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_consultation_list;
    }

    @Override
    protected void initView() {
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        lv = (ListView) findViewById(R.id.lv);
        noDataTv = (TextView) findViewById(R.id.no_data_tv);

    }

    @Override
    protected void initData() {
        api.getMyConsultation(Constant.pageSize, pageNum).enqueue(new NetCallBack<Consultation>() {
            @Override
            public void onResponse(Call<Consultation> call, Response<Consultation> response) {
                if (response.body() == null) {
                    return;
                }

                data = response.body().getRows();
                if (data.size() > 0){
                    lv.setAdapter(new LvViewHolder(data, context));
                }else {
                    noDataTv.setVisibility(View.VISIBLE);

                }
            }
        });
    }

    @Override
    protected void initEvent() {
        radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            if (data.size() > 0) {
                switch (checkedId) {
                    case R.id.rb_not:
                        lv.setAdapter(new LvViewHolder(
                                data.stream().filter(data -> Integer.parseInt(data.getState()) == 0).collect(Collectors.toList()),
                                context
                        ));
                        break;
                    case R.id.rb_in:
                        lv.setAdapter(new LvViewHolder(
                                data.stream().filter(data -> Integer.parseInt(data.getState()) == 1).collect(Collectors.toList()),
                                context
                        ));
                        break;
                    case R.id.rb_over:
                        lv.setAdapter(new LvViewHolder(
                                data.stream().filter(data -> Integer.parseInt(data.getState()) == 2).collect(Collectors.toList()),
                                context
                        ));
                        break;
                }
            }else {
                noDataTv.setVisibility(View.VISIBLE);
            }
        });

        lv.setOnItemClickListener((parent, view, position, id) -> {
            final Consultation.RowsBean item = (Consultation.RowsBean) parent.getAdapter().getItem(position);

            final Intent intent = new Intent(context, ConsultationDetailActivity.class);
            intent.putExtra("data",gson.toJson(item));
            startActivity(intent);

        });
    }

    @Override
    protected String getActionBarTitle() {
        return "我的咨询列表";
    }

    static class LvViewHolder extends CommonAdapter<Consultation.RowsBean, CommonAdapter.CommListViewHolder> {

        public LvViewHolder(List<Consultation.RowsBean> data, Context context) {
            super(data, context);
        }

        @Override
        protected void setData(Consultation.RowsBean data, int position, CommListViewHolder viewHolder, View convertView, Context context) {
            final String[] split = data.getImageUrls().split(",");
            if (split.length > 0) {
                Glide.with(context)
                        .load(Constant.BASE_URL + split[0])
                        .into(viewHolder.im);
            }

            viewHolder.title.setMaxLines(2);
            viewHolder.title.setEllipsize(TextUtils.TruncateAt.END);
            viewHolder.title.setText(String.valueOf(data.getContent()));
            viewHolder.content.setText("受理律师：" + String.valueOf(data.getLawyerName()));
            viewHolder.tag1.setText("律师专长：" + data.getLegalExpertiseName());
            viewHolder.tag2.setText("受理状态：" + Constant.getConsultationState(Integer.parseInt(data.getState())));
            viewHolder.tag3.setText("提交时间：" + data.getCreateTime());


        }

        @Override
        protected CommListViewHolder getViewHolder() {
            return new CommListViewHolder();
        }

        @Override
        protected void initView(CommListViewHolder viewHolder, View convertView) {
            viewHolder.title = (TextView) convertView.findViewById(R.id.title);
            viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
            viewHolder.content = (TextView) convertView.findViewById(R.id.content);
            viewHolder.tag1 = (TextView) convertView.findViewById(R.id.tag1);
            viewHolder.tag2 = (TextView) convertView.findViewById(R.id.tag2);
            viewHolder.tag3 = (TextView) convertView.findViewById(R.id.tag3);

        }

    }


}